<?php
// +----------------------------------------------------------------------
// | Qbt CMS Plugin
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.qbt8.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 千佰特 <http://www.qbt8.com>
// +----------------------------------------------------------------------
namespace plugins\qbt_mobile_theme;
use cmf\lib\Plugin;

class QbtMobileThemePlugin extends Plugin
{
    public $info = [
        'name'        => 'QbtMobileTheme',
        'title'       => '手机端模板控制',
        'description' => '手机端模板控制',
        'status'      => 1,
        'author'      => '千佰特',
        'version'     => '1.0',
        'demo_url'    => 'http://www.qbt8.com',
        'author_url'  => 'http://www.qbt8.com'
    ];

    public $hasAdmin = 0;

    public function install()
    {
        return true;
    }

    public function uninstall()
    {
        return true;
    }

    public function switchTheme($param)
    {
        $config = $this->getConfig();
        $regex = '/android|adr|iphone|ipad|windows\sphone|kindle|gt\-p|gt\-n|rim\stablet|opera|meego/i';
        $mobile = false;
        if ($this->GetVars('alwaystheme', 'COOKIE') == 'mobile') {
            $mobile = true;
        }
        if (preg_match($regex, $this->GetVars('HTTP_USER_AGENT', 'SERVER'))) {
            $mobile = true;
        }
        if ($this->GetVars('alwaystheme', 'COOKIE') == 'pc') {
            $mobile = false;
        }


        if ($mobile) {
            $cmfDefaultTheme = $config['mobilethems'];
        }else{
            $cmfDefaultTheme = config('cmf_default_theme');
        }
        return $cmfDefaultTheme;
    }

    private function GetVars($name, $type = 'REQUEST') {
        $array = &$GLOBALS[strtoupper("_$type")];
        if (isset($array[$name])) {
            return $array[$name];
        } else {
            return null;
        }
    }
}